package com.slw.caiji.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slw.caiji.Vo.SysTemplateVo;
import com.slw.caiji.dto.SysTemplate;
import com.slw.caiji.mapper.SysTemplateMapper;
import com.slw.caiji.service.SysTemplateService;
import com.slw.caiji.utils.ExcelUtils;
import com.slw.caiji.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SysTemplateServiceImpl implements SysTemplateService {

    @Autowired
    private SysTemplateMapper sysTemplateMapper;

    @Override
    public Result getSysTemplateList(SysTemplateVo sysTemplateVo) {
        final Page<SysTemplate> objectPage = new Page<>(sysTemplateVo.getCurrent(), sysTemplateVo.getPageSize());
        final SysTemplate sysTemplate = new SysTemplate();
        sysTemplate.setInput(sysTemplateVo.getInput());
        sysTemplate.setNumberInput(sysTemplate.getNumberInput());
        sysTemplate.setTreeSelect(sysTemplate.getTreeSelect());
        sysTemplate.setDictSelect(sysTemplate.getDictSelect());
        sysTemplate.setTrendsSelect(sysTemplate.getTrendsSelect());
        final IPage<SysTemplate> sysUserIPage = sysTemplateMapper.listSysTemplate(objectPage, sysTemplate);
        return Result.success(sysUserIPage);
    }

    @Override
    public Result delTemplate(String ids) {
        final String[] split = ids.split(",");
        for (String s : split) {
            sysTemplateMapper.deleteById(s);
        }
        return Result.success(1);
    }

    @Override
    public Result detailTemplate(String id) {
        SysTemplate sysTemplate = sysTemplateMapper.selectById(id);
        return Result.success(sysTemplate);
    }

    @Override
    public Result updateTemplate(SysTemplate sysTemplate) {
        SysTemplate sysTemplate1 = new SysTemplate();
        sysTemplate1.setId(sysTemplate.getId());
        sysTemplate1.setInput(sysTemplate.getInput());
        sysTemplate1.setCheckbox(sysTemplate.getCheckbox());
        sysTemplate1.setRadio(sysTemplate.getRadio());
        sysTemplate1.setCustomFile(sysTemplate.getCustomFile());
        sysTemplate1.setOtherFile(sysTemplate.getOtherFile());
        sysTemplate1.setInputTag(sysTemplate.getInputTag());
        sysTemplate1.setPassword(sysTemplate.getPassword());
        sysTemplate1.setNumberInput(sysTemplate.getNumberInput());
        sysTemplate1.setTextarea(sysTemplate.getTextarea());
        sysTemplate1.setTreeSelect(sysTemplate.getTreeSelect());
        sysTemplate1.setDatePicker(sysTemplate.getDatePicker());
        sysTemplate1.setMonthPicker(sysTemplate.getMonthPicker());
        sysTemplate1.setYearPicker(sysTemplate.getYearPicker());
        sysTemplate1.setStartTime(sysTemplate.getStartTime());
        sysTemplate1.setEndTime(sysTemplate.getEndTime());
        if(sysTemplate.getFile() != null) {
            sysTemplate1.setFile(sysTemplate.getFile());
        }
        sysTemplate1.setDictSelect(sysTemplate.getDictSelect());
        sysTemplate1.setTrendsSelect(sysTemplate.getTrendsSelect());
        if(sysTemplate.getMap() != null) {
            sysTemplate1.setMap(sysTemplate.getMap());
            sysTemplate1.setLongitude(sysTemplate.getLongitude());
            sysTemplate1.setLatitude(sysTemplate.getLatitude());
        }
        return Result.success(sysTemplateMapper.updateById(sysTemplate1));
    }

    @Override
    public void exportTemplate(SysTemplate sysTemplate, HttpServletResponse response) {
        try {
            final List<SysTemplate> list = sysTemplateMapper.getListSysTemplate(sysTemplate);
            ExcelUtils.write(response, "模板数据.xls", "模板数据", SysTemplate.class, list );
        }catch (IOException exception){
            log.info(String.valueOf(exception));
        }
    }

    @Override
    public Result addTemplate(SysTemplate sysTemplate) {
        final SysTemplate sysTemplate1 = new SysTemplate();
        sysTemplate1.setInput(sysTemplate.getInput());
        sysTemplate1.setCheckbox(sysTemplate.getCheckbox());
        sysTemplate1.setRadio(sysTemplate.getRadio());
        sysTemplate1.setCustomFile(sysTemplate.getCustomFile());
        sysTemplate1.setOtherFile(sysTemplate.getOtherFile());
        sysTemplate1.setInputTag(sysTemplate.getInputTag());
        sysTemplate1.setPassword(sysTemplate.getPassword());
        sysTemplate1.setNumberInput(sysTemplate.getNumberInput());
        sysTemplate1.setTextarea(sysTemplate.getTextarea());
        sysTemplate1.setTreeSelect(sysTemplate.getTreeSelect());
        sysTemplate1.setFile(sysTemplate.getFile());
        sysTemplate1.setDictSelect(sysTemplate.getDictSelect());
        sysTemplate1.setTrendsSelect(sysTemplate.getTrendsSelect());
        sysTemplate1.setDatePicker(sysTemplate.getDatePicker());
        sysTemplate1.setMonthPicker(sysTemplate.getMonthPicker());
        sysTemplate1.setYearPicker(sysTemplate.getYearPicker());
        sysTemplate1.setStartTime(sysTemplate.getStartTime());
        sysTemplate1.setEndTime(sysTemplate.getEndTime());
        if(sysTemplate.getMap() != null) {
            sysTemplate1.setMap(sysTemplate.getMap());
            sysTemplate1.setLongitude(sysTemplate.getLongitude());
            sysTemplate1.setLatitude(sysTemplate.getLatitude());
        }
        return Result.success(sysTemplateMapper.insert(sysTemplate1));
    }

    private List<SysTemplate> sysTemplateList = new ArrayList<>();

    @Override
    public Result importTemplate(MultipartFile file) throws IOException {

        final InputStream inputStream = file.getInputStream();
        EasyExcel.read(inputStream).sheet(0).head(SysTemplate.class).headRowNumber(1).registerReadListener(
                new AnalysisEventListener<SysTemplate>() {
                    @Override
                    public void invoke(SysTemplate sysTemplate, AnalysisContext analysisContext) {
                        log.info("sysTemplate: " + JSON.toJSONString(sysTemplate));
                        sysTemplateList.add(sysTemplate);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        log.info("sysTemplateList: " + JSON.toJSONString(sysTemplateList));
                        for (SysTemplate s:
                                sysTemplateList ) {
                            sysTemplateMapper.insert(s);
                        }
                    }
                }
        ).doRead();
        return Result.success(1);
    }
}
